package zozopo.manual;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Connection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.comm.Validate;
import zozopo.database.BusiDatabaseManager;
import zozopo.manual.operations.ReleaseOS;
import zozopo.manual.operations.ReplyOS;
import zozopo.manual.operations.ReplyTB;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ReplyForAjaxAction extends ActionSupport implements Action,SessionAware,ServletRequestAware,ServletResponseAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	

	private String context;
	private String name;
	private String valiCode;
	private int subjectId;
	



	HttpServletRequest request;
	HttpServletResponse response;
	private Map<Object, Object> session;
	
	public String execute(){
		try {
			UserTB user = (UserTB) session.get("userInfo"); 
			ReplyTB reply = new ReplyTB();
			if(user != null){
				name = user.getUser_name();
				reply.setReplys_kind("REPLY.FROM.INSIDE");
			}else{
				reply.setReplys_kind("REPLY.FROM.OUTSIDE");
			}
			String valicode = (String) session.get("valicode");
			context = context.replaceAll("'", "\\\\'");
			context = Validate.htmlLabelFilter(context);
			
			if(valicode == null || !valicode.equals(valiCode)){
				response.getWriter().write("1");
				//response.getWriter().write("输入检证码不正确,请重新输入");
				return NONE;
			}
			
			if(name.length() > 64){
				response.getWriter().write("2");
				//response.getWriter().write("称呼长度不能大于64个字符");
				return NONE;
			}
			if(context.length() > 800){
				response.getWriter().write("3");
				//response.getWriter().write("内容长度不能大于800个字符");
				return NONE;
			}
			
			BusiDatabaseManager databaseConnection = new BusiDatabaseManager();
			Connection conn = databaseConnection.getConnection();
			
			ReplyOS replyOS = new ReplyOS(conn);
			ReleaseOS  releaseOS = new ReleaseOS(conn);
		
			reply.setReplys_context(this.context);
			reply.setReplys_name(this.name);
			reply.setReplys_ip(getIpAddr(request));
			reply.setReplys_release_id(subjectId);
			releaseOS.reply(subjectId);
			
			replyOS.replySubject(reply);
			
			//加积分
			if(user != null){
				UserOS userOS = new UserOS(conn);
				userOS.addIntegralByEmail(user.getUser_email(), 1);
			}
			
			//清空session valicode
			session.remove("valicode");
			
			response.getWriter().flush();
			databaseConnection.releaseConnection(conn);
		} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				//return ERROR;
		}
		
		return NONE;
	}



	public int getSubjectId() {
		return subjectId;
	}



	public void setSubjectId(int subjectId) {
		this.subjectId = subjectId;
	}



	public String getContext() {
		return context;
	}

	public void setContext(String context) throws UnsupportedEncodingException {
		//saerchStr = java.net.URLDecoder.decode(saerchStr, "UTF-8");
		this.context = URLDecoder.decode(context,"UTF-8");
		//this.context = java.net.URLDecoder.decode(context, "gb2312");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) throws UnsupportedEncodingException {
		this.name = java.net.URLDecoder.decode(name, "UTF-8");
	}

	public String getValiCode() {
		return valiCode;
	}

	public void setValiCode(String valiCode) {
		this.valiCode = valiCode;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		
		request = arg0;
		try {
			request.setCharacterEncoding("gb2312");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}

	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
		response.setCharacterEncoding("GB2312");
		response.setContentType("text/xml; charset=gb2312");
	}
	
	/**
     * 获得客户端真是ip
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("WL-Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getRemoteAddr();    
     }
		return ip; 
    }
}
	
	